package com.karumi.dexter.listener;

import com.karumi.dexter.ResourceTable;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;
/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * 底部弹框
 */
public class PopupDialogUtils {
    /**
     * 实例化
     */
    public static PopupDialogUtils utils = new PopupDialogUtils();
    private PopupDialog popupDialog;

    private PopupDialogUtils() {}

    /**
     * 初始化
     * @return PopupDialogUtils
     */
    public static PopupDialogUtils getInstance() {
        return utils;
    }

    /**
     * 展示
     * @param mContext mContext
     * @param text text
     * @param duration duration
     * @param btnText btnText
     * @param listener listener
     */
    public void show(Context mContext, String text, int duration, String btnText, Component.ClickedListener listener) {
        Component component = new Component(mContext);

        popupDialog =
                new PopupDialog(
                        mContext,
                        component,
                        ComponentContainer.LayoutConfig.MATCH_PARENT,
                        ComponentContainer.LayoutConfig.MATCH_CONTENT);

        component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_dexter_popup_dialog, null, false);

        Text textContent = (Text) component.findComponentById(ResourceTable.Id_txt_content);
        Text txtBtn = (Text) component.findComponentById(ResourceTable.Id_txt_btn);

        textContent.setText(text);
        txtBtn.setText(btnText);

        txtBtn.setClickedListener(
                new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        popupDialog.hide();
                        listener.onClick(component);

                        popupDialog.destroy();
                        popupDialog = null;
                    }
                });

        popupDialog.setTransparent(true);
        popupDialog.setAlignment(LayoutAlignment.BOTTOM);
        popupDialog.setCustomComponent(component);
        popupDialog.show();
    }
}
